{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true,
    "pycharm": {
     "name": "#%% md\n"
    }
   },
   "source": [
    "## 1.元组的定义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [
    "# 元组表示多个元素组成的序列，有特定的应用环境，元素不能进行修改\n",
    "# 用于存储一串信息，数据之间使用，分隔\n",
    "# 元组用（）定义"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(50, 50, 123, 45, 6)\n"
     ]
    }
   ],
   "source": [
    "# 创建空元组\n",
    "info_tuple = ()\n",
    "\n",
    "# 元组中只包含一个元素时，需要在元素后面添加逗号，不加逗号就是整形元素\n",
    "info_tuple = (50,50,123,45,6)\n",
    "print(info_tuple)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 2.元组的常用操作"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n",
      "2\n",
      "123\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "# 获取元组长度\n",
    "print(len(info_tuple))\n",
    "\n",
    "# 数据在元组中出现的次数\n",
    "print(info_tuple.count(50))\n",
    "\n",
    "# 从元组中取值\n",
    "print(info_tuple[2])\n",
    "\n",
    "# 获得数据第一次出现的索引\n",
    "print(info_tuple.index(50))"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 3.循环遍历"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "50\n",
      "50\n",
      "123\n",
      "45\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "for item in info_tuple:\n",
    "    print(item)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 4.格式化字符串"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "小明年龄是21身高是1.85\n",
      "小明年龄是21身高是1.85\n"
     ]
    }
   ],
   "source": [
    "info_tuple = (\"小明\",21,1.85)\n",
    "\n",
    "# 格式化字符串后面的（）本质上就是元组\n",
    "print(\"%s年龄是%d身高是%.2f\"%info_tuple)\n",
    "\n",
    "info_str = \"%s年龄是%d身高是%.2f\"%info_tuple\n",
    "print(info_str)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "markdown",
   "source": [
    "## 5.元组和列表之间的转换"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%% md\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['小明', 21, 1.85] <class 'list'>\n",
      "('小明', 21, 1.85)\n"
     ]
    }
   ],
   "source": [
    "info_list = list(info_tuple)\n",
    "print(info_list,type(info_list))\n",
    "\n",
    "info_tuple = tuple(info_list)\n",
    "print(info_tuple)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}